package com.ruoyi.mall.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.mall.domain.CustomerAddress;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.CustomerAddressVo;
import com.ruoyi.mall.domain.bo.CustomerAddressBo;
import com.ruoyi.mall.service.ICustomerAddressService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员地址
 *
 * @author wolong
 * @date 2025-02-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/customerAddress")
public class CustomerAddressController extends BaseController {

    private final ICustomerAddressService iCustomerAddressService;

    /**
     * 查询会员地址列表
     */
    @SaCheckPermission("mall:customerAddress:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerAddressVo> list(CustomerAddressBo bo, PageQuery pageQuery) {
        return iCustomerAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员地址列表
     */
    @SaCheckPermission("mall:customerAddress:export")
    @Log(title = "会员地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CustomerAddressBo bo, HttpServletResponse response) {
        List<CustomerAddressVo> list = iCustomerAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员地址", CustomerAddressVo.class, response);
    }

    /**
     * 获取会员地址详细信息
     *
     * @param customerAddressId 主键
     */
    @SaCheckPermission("mall:customerAddress:query")
    @GetMapping("/{customerAddressId}")
    public R<CustomerAddressVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long customerAddressId) {
        return R.ok(iCustomerAddressService.queryById(customerAddressId));
    }

    /**
     * 新增会员地址
     */
    @SaCheckPermission("mall:customerAddress:add")
    @Log(title = "会员地址", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CustomerAddressBo bo) {
        return toAjax(iCustomerAddressService.insertByBo(bo));
    }

    /**
     * 修改会员地址
     */
    @SaCheckPermission("mall:customerAddress:edit")
    @Log(title = "会员地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CustomerAddressBo bo) {
        return toAjax(iCustomerAddressService.updateByBo(bo));
    }

    /**
     * 删除会员地址
     *
     * @param customerAddressIds 主键串
     */
    @SaCheckPermission("mall:customerAddress:remove")
    @Log(title = "会员地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerAddressIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] customerAddressIds) {
        return toAjax(iCustomerAddressService.deleteWithValidByIds(Arrays.asList(customerAddressIds), true));
    }


    /**
     * 会员ID获取会员地址列表
     * @return
     */
    @SaIgnore
    @RequestMapping("/getAddressByCustomerId")
    public R<List<CustomerAddressVo>> getAddressByCustomerId()
    {
        LoginUser loginUser = LoginHelper.getLoginUser();
        CustomerAddressBo query = new CustomerAddressBo();
        query.setCustomerId(loginUser.getUserId());
        return R.ok(iCustomerAddressService.queryList(query));
    }


    /**
     * 更新会员地址
     * @return
     */
    @SaIgnore
    @RequestMapping("/updateCustomerAddress")
    public R<Void> updateCustomerAddress(@RequestBody CustomerAddressBo customerAddressBo)
    {
        boolean result = iCustomerAddressService.updateByBo(customerAddressBo);
        if (result)
        {
            return R.ok("更新会员地址成功！");
        }
        return R.fail("更新会员地址失败！");
    }

    /**
     * 根据会员地址ID删除会员地址
     * @param json
     * @return
     */
    @SaIgnore
    @RequestMapping("/deleteCustomerAddressById")
    public R<Void> deleteCustomerAddressById(@RequestBody JSONObject json)
    {
        Long addressId = Long.valueOf(json.get("addressId").toString());
        List<Long> ids = new ArrayList<>();
        ids.add(addressId);
        boolean result =  iCustomerAddressService.deleteWithValidByIds(ids, false);
        if (result)
        {
            return R.ok("删除会员地址成功！");
        }
        return R.fail("删除会员地址失败！");
    }

}
